package com.ldu.houserent.controller.front;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldu.houserent.common.base.BaseController;
import com.ldu.houserent.common.enums.HouseRentTypeEnum;
import com.ldu.houserent.common.util.PageUtil;
import com.ldu.houserent.common.vo.HouseSearchVO;
import com.ldu.houserent.entity.House;
import com.ldu.houserent.entity.Order;
import com.ldu.houserent.entity.User;
import com.ldu.houserent.service.HouseService;
import com.ldu.houserent.service.OrderService;
import com.ldu.houserent.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 前端房屋信息控制器
 */
@Controller("frontHouseController")
public class HouseController extends BaseController {

    @Autowired
    private HouseService houseService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    /*
        房屋详情
     */
    @RequestMapping("/house/detail/{id}")
    public String houseDetail(@PathVariable("id")Long id, Model model){
        //根据id查询房屋
        House house=houseService.get(id);
        if (house==null){   //找不到房屋信息
            return renderNotFound();        //转向404页面
        }
        //处理轮播图url  将json格式转换成字符串
        List<String> slideList=JSON.parseArray(house.getSlideUrl(),String.class);
        house.setSlideImgList(slideList);
        //如果是合租，则查询合租房屋
        List<House> shareHouseList = houseService.findByUserIdAndCetificateNoAndRentType(house.getUserId(),house.getCetificateNo(), HouseRentTypeEnum.SHARE.getValue());
        //遍历循环  从订单中查找到租客id，把租客放到每个房屋里     显示合租信息用的if判断
        if (shareHouseList!=null && shareHouseList.size()>0){
            for (House houseTemp: shareHouseList){      //给shareHouseList取别名为houseTemp
                Order currentOrder = orderService.getCurrentEffectiveOrder(houseTemp.getId());      //查询当前房屋的有效订单（未到期）
                if (currentOrder!=null){    //如果有效订单不为空
                    User customerUser=userService.get(currentOrder.getCustomerUserId());      //根据租客的id进行查询
                    currentOrder.setCustomerUser(customerUser);
                    houseTemp.setCurrentOrder(currentOrder);
                }
            }
        }
        house.setShareHouseList(shareHouseList);    //存放到house中
        model.addAttribute("house",house);
        return "front/house-detail";
    }

    /*
        房屋地图页面
     */
    @RequestMapping("/house/map")
    public String map(@RequestParam("id")Long id,Model model){      //查询到房屋所在的经纬度，用model传到前端
        //根据id查询房屋
        House house=houseService.get(id);
        if (house==null){   //找不到房屋信息
            return renderNotFound();        //转向404页面
        }
        String longitudeLatitude = house.getLongitudeLatitude();
        String[] arr=longitudeLatitude.split(",");
        model.addAttribute("longitude",arr[0]);     //经度
        model.addAttribute("latitude",arr[1]);      //纬度
        model.addAttribute("address",house.getAddress());
        return "front/house-map";
    }//map

    /*
        房屋列表
     */
    @RequestMapping("/house")
    public String houseList(HouseSearchVO houseSearchVO,Model model){
        //实现分页功能
        Page page = PageUtil.initMpPage(houseSearchVO.getPage(),houseSearchVO.getSize());       //两个参数：到第几页了，每一页的大小
        Page<House> housePage = houseService.getHousePage(houseSearchVO,page);
        model.addAttribute("pageInfo",housePage);
        model.addAttribute("houseSearchVO",houseSearchVO);      //将查询条件传回，方便前端再次显示
        model.addAttribute("pagePrefix",houseSearchVO.getPagePrefix());
        return "front/house-list";
    }


}
